#include <stdlib.h>
#include <qwt_painter.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_marker.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>
#include <qwt_scale_widget.h>
#include <qwt_legend.h>
#include <qwt_scale_draw.h>
#include <qwt_math.h>
#include "data_plot.h"
#include <iostream>

double valores[20000];

//
//  Initialize main window
//
DataPlot::DataPlot(QWidget *parent, Algoritmos* b):
    QwtPlot(parent),
    d_interval(0),
    d_timerId(-1)
{
    datos = b;
    
	for(int i=0; i< 1000; i++)
	{
		valores[i] = sin(50*i)* (-1.0 + 0.5 * double(rand()) / double(RAND_MAX));
	}
	
	
    // Disable polygon clipping
    QwtPainter::setDeviceClipping(false);

    // We don't need the cache here
    canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
    canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);

#if QT_VERSION >= 0x040000
#ifdef Q_WS_X11
    /*
       Qt::WA_PaintOnScreen is only supported for X11, but leads
       to substantial bugs with Qt 4.2.x/Windows
     */
    canvas()->setAttribute(Qt::WA_PaintOnScreen, true);
#endif
#endif

    //alignScales();
    
    //  Initialize data
    for (int i = 0; i< PLOT_SIZE; i++)
    {
        d_x[i] = 0.5 * i;     // time axis
        d_a[i] = 0;        
        d_b[i] = 0;
        d_c[i] = 0;
    }

    // Assign a title
    setTitle("Vectors");
    insertLegend(new QwtLegend(), QwtPlot::BottomLegend);

    // Insert new curves
    QwtPlotCurve *cA = new QwtPlotCurve("acel");
    cA->attach(this);
	QwtPlotCurve *cB = new QwtPlotCurve("vel");
    cB->attach(this);
	QwtPlotCurve *cC = new QwtPlotCurve("pos");
    cC->attach(this);    

    // Set curve styles
    cA->setPen(QPen(Qt::red));
    cB->setPen(QPen(Qt::blue));
    cC->setPen(QPen(Qt::green));  
    // Attach (don't copy) data. Both curves use the same x array.
    cA->setRawData(d_x, d_a, PLOT_SIZE);
    cB->setRawData(d_x, d_b, PLOT_SIZE);
    cC->setRawData(d_x, d_c, PLOT_SIZE);    
    
    //  Insert zero line
    QwtPlotMarker *mY = new QwtPlotMarker();
    mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
    mY->setLineStyle(QwtPlotMarker::HLine);
    mY->setYValue(datos->getStep());
	mY->setLinePen(QPen(Qt::gray));
    mY->attach(this);
   //  Insert zero line
    QwtPlotMarker *mX = new QwtPlotMarker();
    mX->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
    mX->setLineStyle(QwtPlotMarker::HLine);
    mX->setYValue(0);
	mX->setLinePen(QPen(Qt::gray));
    mX->attach(this);
   //  Insert zero line
    QwtPlotMarker *mZ = new QwtPlotMarker();
    mZ->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
    mZ->setLineStyle(QwtPlotMarker::HLine);
    mZ->setYValue(datos->getStep());
	mZ->setLinePen(QPen(Qt::gray));
    mZ->attach(this);            
	
    // Axis 
    setAxisTitle(QwtPlot::xBottom, "Time/seconds");
    setAxisScale(QwtPlot::xBottom, 0, (PLOT_SIZE-1)/2);

    setAxisTitle(QwtPlot::yLeft, "Values");
    setAxisScale(QwtPlot::yLeft, -3, 3);
       
    setTimerInterval(0.0); 					//apaga el timer
}

void DataPlot::setTimerInterval(double ms)
{
    d_interval = qRound(ms);

    if ( d_timerId >= 0 )
    {
        killTimer(d_timerId);
        d_timerId = -1;
    }
    if (d_interval >= 0 )
        d_timerId = startTimer(d_interval);
}

//  Generate new values 
void DataPlot::timerEvent(QTimerEvent *)
{
    // y moves from left to right:
    // Shift y array right and assign new value to y[0].
	
	while(datos->hayVectoresAceleracion())
	{
	
	//	buf->algoritmos.getPosicion();
		for ( int i = PLOT_SIZE - 1; i > 0; i-- )
		{
		    d_a[i] = d_a[i-1];
		    d_b[i] = d_b[i-1];
		    d_c[i] = d_c[i-1];		    		    		    
		}

		//datos->getPosicion();
		
		    
		d_a[0] = datos->vectorAceleracion() ;
		d_b[0] = datos->vectorVelocidad() * 10;       		       
		d_c[0] = datos->vectorPosicion()* 10 ;       		       		
	}	
	

//	this->clear();			para borrar
    // update the display
    replot();
}
